package com.cc8w.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.OutputStream;
import java.util.Random;

import javax.imageio.ImageIO;

public class VerifyCode {
	
	public static void main(String[] args) {
//		BufferedImage verifyCode = new BufferedImage (200,200,BufferedImage.TYPE_3BYTE_BGR);
//		Graphics g = verifyCode.getGraphics();
//		g.setColor(Color.RED);
//		g.drawLine(0, 0, 100, 100);
//		g.dispose();	
		//参数一：Image类对象 ,参数二：图片格式(非正式),
		//参数三：输出了（也可以是请求响应的response.getOutputStream().write(bytes),这样就输出到页面了）
		//ImageIO.write(verifyCode, "png", output);		
		String code = VerifyCode.getCode();
		System.out.println(code);
		
		int rnum = Tools.randNum(-40, 40);
		System.out.println(rnum);
		
		
	}
	
	//字符串库去掉了1iL0o容易混淆的字符串
	private static final String VERIDY_CODES = "23456789abcdefghjkmnpqrstuvwxyz";
	private static Random random = new Random();
	private BufferedImage verifyCode = null;
	
	
	
	
	/**
	 * 获取验证码字符串(默认返回4个字符)
	 */
	public static String getCode() {
		char[] veridy_codes = VERIDY_CODES.toCharArray();

		int num = 4;//循环4次
		StringBuilder sb = new StringBuilder();
		for(int i=0;i<num;i++) {
			int rd = random.nextInt(veridy_codes.length);
			sb.append(veridy_codes[rd]);
		}
		
		return sb.toString();
	}
	
	/**
	 * 获取验证码字符串
	 */
	public static String getCode(int num) {
		
		char[] veridy_codes = VERIDY_CODES.toCharArray();

		StringBuilder sb = new StringBuilder();
		for(int i=0;i<num;i++) {
			int rd = random.nextInt(veridy_codes.length);
			sb.append(veridy_codes[rd]);
		}
		
		return sb.toString();
	}
	
	/**
	 * 颜色设置
	 */
	
	
	/**
	 * 输出验证码
	 */
	public static void outputVerify(int width,int height,String code,OutputStream op) {
		
        BufferedImage verifyCode = new BufferedImage (width,height,BufferedImage.TYPE_3BYTE_BGR);
        Graphics2D g2 = (Graphics2D)verifyCode.getGraphics();

		verifyCode = VerifyCode.codePaintoImage(code, verifyCode);
		
		
	}
	
	/**
	 * 把随机生成的验证码字符串打印到image接口实现类的画布上
	 */
	public static BufferedImage codePaintoImage(String code,BufferedImage bfimg) {
		Graphics2D  g2 =  (Graphics2D) bfimg.getGraphics();
		int pintW = 10;
		int pintH = 100;
		char[] ch = code.toCharArray();
		for(int i=0;i<ch.length;i++) {
			//设置字体(字体,颜色,坐标,应该随机)
			pintW = pintW+40;
			g2.setColor(Color.CYAN );
			g2.setFont(new Font("Algerian",Font.BOLD,30));
			int angle = Tools.randNum(-15, 15);
			
			g2.rotate(Math.toRadians(angle), pintW,pintH);//字符旋转
			
			g2.drawString(String.valueOf(ch[i]), pintW, pintH);
			
			g2.rotate(Math.toRadians(-angle), pintW,pintH);//旋转角度复位(为下一次服务)

		}
		g2.dispose();
		return bfimg;	
		

		
	}
	
	/**
	 * 加干扰多钟
	 */
	
	public static BufferedImage addGr(BufferedImage bfimg) {
		Graphics2D  g2 =  (Graphics2D) bfimg.getGraphics();
		
		Color[] r = {Color.RED,Color.BLUE,Color.cyan,Color.darkGray,Color.GREEN,Color.ORANGE,Color.PINK,Color.orange};
		
		//1.随机化椭圆,圆
		for(int i=0;i<4;i++) {
			int x = Tools.randNum(50, 150);
			int y = Tools.randNum(50, 150);
			int width = Tools.randNum(50, 150);
			int height = Tools.randNum(50, 150);
			g2.setColor(r[Tools.randNum(1, r.length-1)]);
			
			g2.drawOval(x, y, width, height);
		}
		
		
	
		//3.随机化直线
		//4.随机化波浪线
		for(int i=0;i<8;i++) {
			int x1 = Tools.randNum(10, 150);
			int y1 = Tools.randNum(50, 150);
			int x2 = Tools.randNum(10, 150);
			int y2 = Tools.randNum(50, 150);
			g2.setColor(r[Tools.randNum(1, r.length-1)]);
			g2.drawLine(x1,y1,x2 ,y2);
		}
		
		
		
		g2.dispose();
		return bfimg;
	}
	

}
